package org.csmzxy.class2333;

public class Example03 {
    public static void main(String[] args) {
        int sum = sumOfFibonacci(20);
        System.out.println("斐波那契那数列前20项之和是：" + sum);
    }
    public static int sumOfFibonacci(int n) {
        if (n <= 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        int first = 1, second = 1, next = 0, sum = 2;
        for (int i = 3; i <= n; i++) {
            next = first + second;
            sum += next;
            first = second;
            second = next;
        }
        return sum;
    }
}
